function Server() {}

Server.prototype = {
  os: Components.classes["@mozilla.org/xre/app-info;1"].getService(Components.interfaces.nsIXULRuntime).OS,
  port: String(Math.floor(Math.random() * (3999 - 3701 + 1)) + 3701),
  
  start: function() {
    if (this.os == "WINNT") {
      this.run(this.bin("c:\\lark\\hstart.exe"), ["/NOWINDOW", "/WAIT", "c:\\lark\\ruby\\bin\\ruby c:\\lark\\ruby\\bin\\mongrel_rails service::install -N lark" + this.port + " -c " + this.application().path + " -p " + this.port, "net start lark" + this.port]);
    } else {
      this.run(this.bin("/bin/bash"), [this.script("start.sh").path, this.port]);
    }
  },
  
  stop: function() {
    if (this.os == "WINNT") {
      this.run(this.bin("c:\\lark\\hstart.exe"), ["/NOWINDOW", "/WAIT", "net stop lark " + this.port, "c:\\lark\\ruby\\bin\\ruby c:\\lark\\ruby\\bin\\mongrel_rails service::remove -N lark" + this.port]);
    } else {
      this.run(this.bin("/bin/bash"), [this.script("stop.sh").path]);
    }
  },
  
  script: function(name) {
    var script = Components.classes["@mozilla.org/file/directory_service;1"]
      .getService(Components.interfaces.nsIProperties)
      .get("CurProcD", Components.interfaces.nsIFile);
    script.append("..");
    script.append("Frameworks");
    script.append("Lark.framework");
    script.append("Ruby");
    script.append("bin");
    script.append(name);
    return script;
  },
  
  application: function() {
    var script = Components.classes["@mozilla.org/file/directory_service;1"]
      .getService(Components.interfaces.nsIProperties)
      .get("CurProcD", Components.interfaces.nsIFile);
    script.append("application");
    return script;
  },
  
  bin: function(name) {
    var bin = Components.classes["@mozilla.org/file/local;1"]
      .createInstance(Components.interfaces.nsILocalFile);
    bin.initWithPath(name);
    return bin;
  },
  
  run: function(bin, args) {
    var process =  Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
    process.init(bin); 
    process.run(true, args, args.length);
  }
};